package dialogs;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import controllers.AbstractController;

@SuppressWarnings("serial")
public abstract class AbstractGeneralPurposeDialog extends AbstractDialog {

	public AbstractGeneralPurposeDialog(JFrame parent, String title,
			String table, String[] labels, AbstractController c) {
		super(parent, title, table, labels, c);
	}
	
	protected void insertButtons(JPanel buttonPane)
	{
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
		buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 2));

		JButton OKButton = new JButton("OK");
		JButton cancelButton = new JButton("Cancel");
		OKButton.addActionListener(this);
		OKButton.setActionCommand("OK");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close_dialog();
			}
		});

		// add the buttons to buttonPane
		buttonPane.add(Box.createHorizontalGlue());
		buttonPane.add(OKButton);
		buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
		buttonPane.add(cancelButton);
	}
	
	
	protected void insertLabelsAndFields(JPanel inputPane, String[] labels) {
		GridBagLayout gb = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		inputPane.setLayout(gb);

		for (String l : labels) {
			// create and place branch id label
			JLabel label = new JLabel(l, SwingConstants.RIGHT);
			c.gridwidth = GridBagConstraints.RELATIVE;
			c.insets = new Insets(0, 0, 0, 5);
			c.anchor = GridBagConstraints.EAST;
			gb.setConstraints(label, c);
			inputPane.add(label);

			JTextField temp;
			//Handle situation where the field is a password field
			if (l.toLowerCase().contains("password"))
			{
				temp = (new JPasswordField(10));
				((JPasswordField)temp).setEchoChar('*');
			}
			else temp = new JTextField(10);
			
			text_fields.add(temp);
			// place branch id field
			c.gridwidth = GridBagConstraints.REMAINDER;
			c.insets = new Insets(0, 0, 0, 0);
			c.anchor = GridBagConstraints.WEST;
			gb.setConstraints(temp, c);
			inputPane.add(temp);
		}

		// when the return key is pressed in the last field
		// of this form, the action performed by the ok button
		// is executed
		text_fields.lastElement().addActionListener(this);
		text_fields.lastElement().setActionCommand("OK");
	}

}
